<?php

namespace App\Services\UpdateUserInfo;

use App\Models\User;
use Illuminate\Http\Request;

class UpdateUserInfo
{
	protected Request $request;

	public function __construct(Request $request)
	{
		$this->request = $request;
	}

	protected function updateData()
	{
        $request = $this->request;

        /**
         * @var User $user
         */
        $user = $request->user();

        $data = [
            'name' => $request->name ?? $user->name,
            'avatar' => $request->avatar ?? $user->avatar,
            'signature' => $request->signature ?? $user->signature,
            'gender' => $request->gender ?? $user->gender,
            'birthday' => $request->birthday ?? $user->birthday,
            'alipay_user_no' => $request->alipay_user_no ?? $user->alipay_user_no,
            'is_locked' => $request->is_locked ?? $user->is_locked,
            'address' => $request->address ?? $user->address,
            'wx_headimgurl'  => $request->wx_headimgurl ?? $user->wx_headimgurl,
            'wx_city'=>$request->wx_city ?? $user->wx_city,
            'longitude'=>$request->longitude ?? $user->longitude,
            'latitude'=>$request->latitude ?? $user->latitude,
        ];

        if ($parent = $request->get('parent', null)) {
            $data['parent_id'] = $parent->id;
        }

		$user->update($data);
	}

	public function update()
	{
		$this->updateData();
	}
}
